// pages/xunpan-reply/index.js
// import http from "../../../../class/Utils/Http"
import tips from "../../../../class/Utils/Tips"



Page({
    data: {
      mp_code:'200',
      mp_info: { shouji: '13940563477',},    
      apic: 'https://toocle.y.netsun.com/Uploads/head/5bdbe3d11a6d7.png',
      bpic:'http://img4.imgtn.bdimg.com/it/u=1561048058,1990782638&fm=26&gp=0.jpg',
      weixin:'',
        interval: "",
        mingpian_s: 0,
        jiazai_s:0,
        // talklist: [],
        talklist: [
          { time: '2018-05-02 16:42:30', sender: '1', body: '嘘！小点声别吓到别人。嘘！小点声别吓到别人。嘘！小点声别吓到别人。', weidustate:'0'},
          { time: '2018-05-02 16:42:30', sender: '2', body: '汪汪汪汪汪汪汪汪汪汪汪汪汪汪汪汪汪汪汪汪汪汪汪汪汪汪汪' , weidustate: '1'},
        ]
    },
    /*询盘 思路  将  auserid  buserid  uuuserid  传输到 后台*/
    onLoad: function (options) {
        var that = this
        var user = wx.getStorageSync('user');
        that.setData({
            user: user
        })

        // wx.showLoading({
        //     title: '加载中',
        // })
        /*
        sender : 本人
         receiver:对方
         */
        var post_data = {
            sender:options.sender,
            receiver:options.receiver
        }
        // http.req("/Mingpian/xiaoxi_frind_list",post_data, function (ressss) {
        //     console.log(ressss)

        //         var list = ressss.data
        //         // for(var i=0;i<list.length;i++){
        //         //     if(list[i].userid == auserid){
        //         //         list[i].touxiang = that.data.mp_info.pic
        //         //     }
        //         // }
        //         that.setData({
        //             talklist: ressss.msg,
        //             apic: ressss.apic,
        //             bpic: ressss.bpic,
        //             mp_info:ressss.mp_info,
        //             mp_code:ressss.mp_info_code,
        //             offset:ressss.offset,
        //             post_data:post_data
        //         })
        //         setTimeout(function () {
        //             that.ObtainpageHeight()
        //         }, 500) //延迟时间 这里是0.5秒

        //     that.setData({
        //         mid: ressss.mid
        //     })
        //     wx.hideLoading()

        // })

        var interval = setInterval(function () {
            var thatlist = that.data.talklist
                console.log('开始轮询')
            var l_post = {
                uid:user.userid,name:options.receiver
            }

//             http.req("/Mingpian/lunxun_xiaoxi_list",l_post, function (res) {
//                     console.log(res,l_post)
//                 if(res.ddd){
// console.log("222222222222222")
//                     for(var i=0;i<res.ddd.length;i++){
//                         var text_arr = {
//                             sender:options.receiver,
//                             body: res.ddd[i].msg,
//                             time: res.ddd[i].time,
//                             touxiang: that.data.apic
//                         }
//                         console.log(text_arr)
//                         thatlist.push(text_arr)
//                         console.log(thatlist)
//                     }

//                     that.setData({
//                         talklist: thatlist
//                     })
//                     setTimeout(function () {
//                         that.ObtainpageHeight()
//                     }, 500) //延迟时间 这里是0.5秒
//                 }
//             })

        }, 2000)
        that.setData({
            interval:interval
        })




    },
    onUnload:function () {
      console.log("关闭页面 chat")
        var interval = this.data.interval;
        clearInterval(interval)

    },
    jiazai: function () {
        var that = this
        var post_data = {
            sender:that.data.post_data.sender,
            receiver:that.data.post_data.receiver,
            time:that.data.offset
        }
        console.log(post_data)
        // http.reqload("/Mingpian/xiaoxi_frind_list",post_data, function (ressss) {
        //     console.log(ressss)

        //     var list = ressss.msg
        //     var thatlist = that.data.talklist
        //     if(list.length >0){
        //         list.reverse()
        //         for(var i=0;i<list.length;i++){
        //             thatlist.unshift(list[i])
        //         }

        //     }else{
        //         that.setData({
        //             jiazai_s:1
        //         })
        //     }
        //     that.setData({
        //         talklist: thatlist,
        //         apic: ressss.apic,
        //         bpic: ressss.bpic,
        //         mp_info:ressss.mp_info,
        //         mp_code:ressss.mp_info_code,
        //         offset:ressss.offset,
        //         post_data:post_data
        //     })
        // })
    },
    onReady: function () {

    },
    onShow: function () {
        var that = this;
        that.ObtainpageHeight();
    },
    //复制微信号
    
  copyweixin: function (e) {
        var that = this;
        var thatweixinhao = that.data.weixin
    console.log(thatweixinhao)


    if (thatweixinhao == '' || thatweixinhao == 'undefined' || thatweixinhao == 'unll'){

      wx.showModal({
        title: '提示',
        showCancel: false,
        content: '没有微信号',
        success: function (res) {
          if (res.confirm) {
            console.log('确定')
          } else if (res.cancel) {
            console.log('取消')
          }
        }
      })



        }else{
          
            wx.setClipboardData({
              data: that.data.weixin,
              success: function (res) {
                // that.setData({copyTip:true}),
                console.log("success" + res.data)
              },
              fail: function (res) {
                wx.showModal({
                  title: '提示',
                  showCancel:false,
                  content: '没有微信号',
                  success: function (res) {
                    if (res.confirm) {
                      console.log('确定')
                    } else if (res.cancel) {
                      console.log('取消')
                    }
                  }
                })
              }
            });
        }




        
    },
    //添加到联系人
    phoneNumTap: function () {
        var that = this;
        // 提示呼叫号码还是将号码添加到手机通讯录
        wx.showActionSheet({
            itemList: ['呼叫', '添加联系人'],
            success: function (res) {
                if (res.tapIndex === 0) {
                    // 呼叫号码
                    wx.makePhoneCall({
                        phoneNumber: that.data.mp_info.shouji,
                    })
                } else if (res.tapIndex == 1) {
                    // 添加到手机通讯录
                    wx.addPhoneContact({
                        //lastName: that.data.lastName,
                        photoFilePath: that.data.mp_info.pic,//头像
                        firstName: that.data.mp_info.xingming,//联系人姓名
                        organization: that.data.mp_info.gongsi,//公司名称
                        title: that.data.mp_info.zhiwei,//公司职位
                        mobilePhoneNumber: that.data.mp_info.shouji,//联系人手机号
                        weChatNumber: that.data.mp_info.weixin,//微信号
                        email: that.data.mp_info.youxiang,//电子邮箱
                        workAddressCity: that.data.mp_info.dizhi,//工作城市
                    })
                }
            }
        })
    },
    //点击图片放大
    previewImage: function (e) {
        console.log(e)
        var that = this;
        var current = e.currentTarget.dataset.src;
        wx.previewImage({
            current: current, // 当前显示图片的http链接
            urls: [current] // 需要预览的图片http链接列表
        })
    },
    //查看联系人询盘信息
    contactinfo: function () {
        var that = this
        var continfoa = that.data.continfo
        if (continfoa == 0) {
            that.setData({
                continfo: 1
            })
        } else {
            that.setData({
                continfo: 0
            })
        }
    },

    //获取 input 内容
    Obtaininputtext: function (e) {
        var that = this
        that.setData({
            inputtext: e.detail.value
        })
        //console.log(e.detail.value)
    },
    //输入文字发送
    uploadtext: function () {
        var that = this

        var user = wx.getStorageSync('user');
        //获取输入的文字转化为数组
        var inputtextinfo = that.data.inputtext
        if (!inputtextinfo) {
            tips.error('不能发送空信息')
            return
        }

        //获取发送文字的时间转化为数组
        var that = this
        // 时间
        var newDate = new Date()
        // var timeStamp = newDate.getTime(); //获取时间戳
        var date = newDate.setMinutes(newDate.getMinutes());
        newDate = new Date(date);
        var year = newDate.getFullYear();
        var month = newDate.getMonth() + 1;
        var day = newDate.getDate();
        var h = newDate.getHours();
        var m = newDate.getMinutes();
        var s = newDate.getSeconds();
        if (month < 10) {
            month = '0' + month;
        }
        ;
        if (day < 10) {
            day = '0' + day;
        }
        ;
        if (h < 10) {
            h = '0' + h;
        }
        ;
        if (m < 10) {
            m = '0' + m;
        }
        ;
        if (s < 10) {
            s = '0' + s;
        }
        ;
        var timesz = year + '-' + month + '-' + day + ' ' + h + ':' + m + ':' + s;


        /*
         添加文字留言
         */


        var formdata = {
            uid: user.userid,
            receiver: that.data.post_data.receiver,
            content: inputtextinfo
        }
        http.req("/Mingpian/add_xiaoxi", formdata,function (res) {
            console.log(res)
            if(res.code == 222){
                //转化数组
                var text_arr = {
                    body: inputtextinfo,
                    time: timesz,
                    touxiang: that.data.apic
                }

                var newtalklist = that.data.talklist
                newtalklist.push(text_arr)
                that.setData({
                    talklist: newtalklist
                })

                that.setData({
                    inputtextzi: ''
                })

                that.ObtainpageHeight()
            }
            if(res.code==437){
                tips.error("消息内容过长")
            }
        })
        that.setData({
            inputtext: ''
        })
    },
    //选择图片发送
    uploadimg: function () {
        var that = this;

        var user = wx.getStorageSync('user');
        //获取发送文字的时间转化为数组
        var that = this
        // 时间
        var newDate = new Date()
        // var timeStamp = newDate.getTime(); //获取时间戳
        var date = newDate.setMinutes(newDate.getMinutes());
        newDate = new Date(date);
        var year = newDate.getFullYear();
        var month = newDate.getMonth() + 1;
        var day = newDate.getDate();
        var h = newDate.getHours();
        var m = newDate.getMinutes();
        var s = newDate.getSeconds();
        if (month < 10) {
            month = '0' + month;
        }
        ;
        if (day < 10) {
            day = '0' + day;
        }
        ;
        if (h < 10) {
            h = '0' + h;
        }
        ;
        if (m < 10) {
            m = '0' + m;
        }
        ;
        if (s < 10) {
            s = '0' + s;
        }
        ;
        var timesz = year + '-' + month + '-' + day + ' ' + h + ':' + m + ':' + s;
        var formdata = {
            userid: user.userid,
            mid: that.data.mid,
            type: 2,
            touxiang: that.data.user.wx_img
        }
        var rootDocment = app.httpurl.url
        wx.chooseImage({
            count: 1, // 选择图片的数量，最多9
            sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
            sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有

            success: function (res) {
                // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
                var tempFilePaths = res.tempFilePaths
                wx.uploadFile({
                    url: rootDocment + '/Mingpian/add_liuyan',
                    filePath: tempFilePaths[0],
                    name: 'val', //文件对应的参数名字(key)
                    formData: formdata,  //其它的表单信息
                    success: function (suc) {
                        console.log(suc)
                    },
                })

                //转化为数组
                var img_arr = {
                    val: tempFilePaths,
                    type: 2,
                    ntype: 1,
                    time: timesz,
                    touxiang: that.data.user.wx_img
                }
                var newtalklist = that.data.talklist
                newtalklist.push(img_arr)

                that.setData({
                    talklist: newtalklist
                })

                that.ObtainpageHeight()
            }
        })
    },
    //获取页面高度到底部
    ObtainpageHeight: function () {
        //滚动到页面最底部
        wx.createSelectorQuery().selectAll('.el-xunpan-textul').boundingClientRect(function (rects) {
            rects.forEach(function (rect) {
                wx.pageScrollTo({
                    scrollTop: rect.height,
                    duration:0
                })
            })
        }).exec()
    },
    //获取当前时间
    dataTime: function () {
        var that = this
        // 时间
        var newDate = new Date()
        // var timeStamp = newDate.getTime(); //获取时间戳
        var date = newDate.setMinutes(newDate.getMinutes());
        newDate = new Date(date);
        var year = newDate.getFullYear();
        var month = newDate.getMonth() + 1;
        var day = newDate.getDate();
        var h = newDate.getHours();
        var m = newDate.getMinutes();
        var s = newDate.getSeconds();
        if (month < 10) {
            month = '0' + month;
        }
        ;
        if (day < 10) {
            day = '0' + day;
        }
        ;
        if (h < 10) {
            h = '0' + h;
        }
        ;
        if (m < 10) {
            m = '0' + m;
        }
        ;
        if (s < 10) {
            s = '0' + s;
        }
        ;
        var time = year + '-' + month + '-' + day + ' ' + h + ':' + m + ':' + s;
        that.setData({
            ttime: time,
        })
        console.log(time)
    }


})